package com.mobilebsc.datamanager;

import java.util.ArrayList;
import java.util.List;

import com.mobilebsc.datatype.EDataType;
import com.mobilebsc.datatype.WeakSignal;
import com.mobilebsc.exception.NotImplementedException;
import com.mobilebsc.httpclient.callback.IAsyncTaskCompleteListener;
import com.mobilebsc.httpclient.callback.WeakSignalRestClientUsage;

public class WeakSignalsDataManager implements IDataManager<WeakSignal>,
IAsyncTaskCompleteListener<List<WeakSignal>> {

	private WeakSignalRestClientUsage restClientUsage;
	private List<WeakSignal> resultList;
	private List<WeakSignal> singleResultList;
	private IDataReadyListener dataReadyListener;

	public WeakSignalsDataManager(IDataReadyListener dataReadyListener) {
		restClientUsage = new WeakSignalRestClientUsage(this);
		resultList = null;
		singleResultList = null;
		this.dataReadyListener = dataReadyListener;
	}
	
	@Override
	public void onTaskComplete(List<WeakSignal> result) {
		resultList = result;

		// do something with ws response
		dataReadyListener.dataReady();
	}

	@Override
	public void getAll() {
		restClientUsage.getAll();
	}

	@Override
	public void getById(int id) {
		restClientUsage.getById(id);
	}

	@Override
	public List<WeakSignal> loadAll() {
		if (resultList != null) {
			return resultList;
		} else {
			restClientUsage.getAll();
			return new ArrayList<WeakSignal>();
		}
	}

	@Override
	public List<WeakSignal> loadById(int id) {
		if (resultList != null) {
			for (WeakSignal ws : resultList) {
				if (id == ws.getId()) {
					singleResultList = new ArrayList<WeakSignal>();
					singleResultList.add(ws);
					break;
				}
			}
		} else {
			restClientUsage.getAll();
			singleResultList = new ArrayList<WeakSignal>();
		}

		return singleResultList;
	}

	@Override
	public void add(WeakSignal element) {
		restClientUsage.add(element);
		
	}

	@Override
	public void delete(WeakSignal element) {
		throw new NotImplementedException();
		
	}
	
	public void getReferencedWeakSignals(int referenceId, EDataType referenceType) {
		restClientUsage.getReferencedWeakSignals(referenceId, referenceType);
	}
}